package com.ruoyi.quartz.race.tool;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.github.pagehelper.util.StringUtil;
import com.ruoyi.quartz.util.SSLClient;

/**
 * 获取所有赛事及当前赛季
 */
public class TrendUtilAppend {
	public static void main(String[] args) {
		String id = "1075698";
		getHtml(id);
	}

	public static String getHtml(String id) {
		String url = "https://odds.500.com/fenxi1/ouzhi.php?id=" + id + "&ctype=1&r=1&style=0&guojia=0&chupan=1";
		String result = "";
		for (int i = 0; i <= 5; i++) {
			String turl = url + "" + "&start=" + ((i * 30) + 30);
			System.out.println(turl);
			String tresult = doGet(turl, "utf-8");
			if (StringUtil.isNotEmpty(tresult)) {
				result = result + tresult;
			} else {
				break;
			}
		}
		return result;
	}

	private static String doGet(String url, String charset) {
		HttpClient httpClient = null;
		try {
			if (url.indexOf("https:") > -1) {
				httpClient = new SSLClient();
			} else {
				httpClient = new DefaultHttpClient();
			}
			HttpGet httpGet = new HttpGet(url);
			httpGet.setHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
			httpGet.addHeader("referer", "https://odds.500.com");
			httpGet.addHeader("sec-fetch-dest", "empty");
			httpGet.addHeader("sec-fetch-mode", "cors");
			httpGet.addHeader("sec-fetch-site", "same-origin");
			httpGet.addHeader("x-requested-with", "XMLHttpRequest");

			HttpResponse response = httpClient.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpGet.abort();
				return "";
			}
			HttpEntity entity = response.getEntity();
			String result = null;
			if (entity != null) {
				result = EntityUtils.toString(entity, charset);
			}
			EntityUtils.consume(entity);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
